package com.guigu.yh.springcloud.controller;

import com.guigu.yh.springcloud.common.JsonResult;
import com.guigu.yh.springcloud.entities.Payment;
import com.guigu.yh.springcloud.service.PaymentService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 支付控制器
 *
 * @Author 张颖辉（yh）
 * @Date 2022/8/10 19:28
 * @Version 1.0
 */
@Slf4j
@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    /**
     * 创建
     *
     * @param payment 付款
     * @return {@link JsonResult }
     * @author zyh
     * @date 2022/08/18
     * @version 1.0.0
     */
    @PostMapping("create")
    public JsonResult create(@RequestBody Payment payment) {
        int i = paymentService.create(payment);
        if (i > 0) {
            return new JsonResult("200", "创建成功,serverPort:" + serverPort, payment);
        }
        return new JsonResult("444", "创建失败,serverPort:" + serverPort, null);
    }

    /**
     * 通过id获取支付对象
     *
     * @param id id
     * @return {@link JsonResult }
     * @author zyh
     * @date 2022/08/18
     * @version 1.0.0
     */
    @GetMapping("get/{id}")
    public JsonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        if (payment != null) {
            return new JsonResult("200", "查询成功,serverPort:" + serverPort, payment);
        }
        return new JsonResult("444", "查询失败,serverPort:" + serverPort, null);
    }

    /**
     * 发现服务信息
     *
     * @return {@link Object }
     * @author zyh
     * @date 2022/08/18
     * @version 1.0.0
     */
    @GetMapping("discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("******** element:{}", service);
        }
//        控制台：
//        ******** element:cloud-payment-service
//        ******** element:cloud-order-service
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }
//        控制台：
//        CLOUD-PAYMENT-SERVICE	10.3.11.219	8001	http://10.3.11.219:8001
//        CLOUD-PAYMENT-SERVICE	10.3.11.219	8002	http://10.3.11.219:8002
        return this.discoveryClient;
    }

    /**
     * 测试自己实现的负载均衡规则
     *
     * @return {@link String }
     * @author 张颖辉（zyh）
     * @date 2022/08/21
     * @version 1.0.0
     */
    @RequestMapping("lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @RequestMapping("testFeignTimeOut")
    public String testFeignTimeOut(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }
}
